// pages/shops/saleshope.js
var topFilterAnimation = wx.createAnimation({
  duration: 300,
  timingFunction: 'linear',
  height: 0,
  top: 0,
  bottom: 0,
  right: 0,
  backgroundColor: '#fff',
  opcity: 0.6
})
var rightFilterAnimation = wx.createAnimation({
  duration: 300,
  timingFunction: 'linear',
  width: 300,
  height: 800,
  top: 0,
  bottom: 0,
  right: 0,
  backgroundColor: '#fff',
  opcity: 0.6
})
var course=require('./model/CourseModel.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationData: '',
    show: false,
    top_show: false,
    topAnimationData: '',
    _filterPTypeFontColor: '#000',
    _filterPEditionFontColor: '#000',
    currentSelectProjectType:0,
    currentSelectProjectEdition: 0,
    currentGradeLevel:0,
    currentReqType:2,//请求类型默认推荐类型
    _showProjectFilter:false,
    currentPageIndex:1,
    projectTypes: [
      { id: 0, projectTypeText: '语文', _selected: true }
    ],
    projectEditions:[
      { id: 0, editionText: '人教版', _selected: true }
    ],
    winHeight:200,
    //下拉数据统一，通过type判断是哪个数据的筛选
    selectorFilter:{
        _type:0,
        list: []//list:[id,text]
    },
    projectTypeTip:"教材科目",
    projectEditionTip:"教材版本",
    courses: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var _title = options.title
    if (_title != null && typeof (_title)!=undefined){
      wx.setNavigationBarTitle({
        title: _title,
      })
    }
    var reqType = options.reqType
    if (reqType==null||reqType==undefined){
      reqType=2
    }
    var projectType = options.projectType
    var gradeLevel = options.gradeLevel
    var showProjectFilter = options.showProjectFilter
    this.setData({ 
      currentSelectProjectType: projectType,
      currentGradeLevel: gradeLevel,
      _showProjectFilter: showProjectFilter==1?true:false,
      currentReqType:reqType
    })
    course.getPorjectTypeList(function (list) {
      that.setData({
        projectTypes: list
      })
    })
    course.getProjectEditionList(function (list) {
      that.setData({
        projectEditions: list
      })
    })
    //1：免费视频/2：推荐视频/3：家长课堂
    course.getCourseList(this.data.currentReqType, this.data.currentGradeLevel,
      this.data.currentSelectProjectType, this.data.currentSelectProjectEdition, this.data.currentPageIndex, 10, function (list) {
        that.setData({
          courses: list
        })
      })
  },
  //切换数据源
  fillSeletorFilter:function(_sourceType){
    var that = this
    if (_sourceType==0){//科目类别数据源
      var filterList = new Array()
      this.data.projectTypes.forEach(function (item, index) {
        var _id = item.id;
        var _text = item.projectTypeText;
        var selected = false;
        if (_id == that.data.projectType) {
          selected = true;
        }
        filterList.push({ id: _id, text: _text, _selected: selected })
      })
      this.setData({
        winHeight: filterList.length*40,
        selectorFilter: { _type: _sourceType, list: filterList}
      })
    }else if(_sourceType==1){//科目版本数据源
      var filterList = new Array()
      this.data.projectEditions.forEach(function(item,index){
        var _id = item.id;
        var _text = item.editionText;
        var selected = false;
        if (_id == that.data.projectType) {
          selected = true;
        }
        filterList.push({ id: _id, text: _text, _selected: selected })
      })
      this.setData({
        winHeight: filterList.length * 40,
        selectorFilter: { _type: _sourceType, list: filterList }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this
    var index=this.data.currentPageIndex+1
    course.getCourseList(this.data.currentReqType, this.data.currentGradeLevel,
      this.data.currentSelectProjectType, this.data.currentSelectProjectEdition, index, 10, function (list) {
        if (list!=null&&list.length>0){
          that.setData({
            courses: that.data.courses.concat(list),
            currentPageIndex: index
          })
        }
      })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  filterWithProjectType: function (event) {
    this.fillSeletorFilter(0)
    if (!this.data.top_show) {
      _openTopAnimation(this,"projectType")
    } else {
      _closeTopAnimation(this, "projectType")
    }
    
  },
  filterWithProjectEdition: function (event) {
    this.fillSeletorFilter(1)
    if (!this.data.top_show) {
      _openTopAnimation(this,"projectEdition")
    } else {
      _closeTopAnimation(this, "projectEdition")
    } 
  },
  /**
   * 销量筛选
   */
  filterWithSales: function (event) {
    var currentColor = this.data._filterSalesFontColor
    if (currentColor === '#000') {
      currentColor = '#FF7256'
    } else {
      currentColor = '#000'
    }
    this.setData({
      _filterSalesFontColor: currentColor
    })
  },
  closeTopAnimation: function (event) {
    _closeTopAnimation(this)
  },
  /**
   * 综合条件筛选
   */
  selectedfilterItem: function (event) {
    var that=this
    var filterId = event.currentTarget.dataset.filterid
    var filterText = event.currentTarget.dataset.text
    var filterSourceType = event.currentTarget.dataset.type
    _closeTopAnimation(this)
    this.data.selectorFilter.list.filter(function (item, index, arr) {
      if (filterId == item.id) {
        item._selected = true
      } else {
        item._selected = false
      }
    })
    if(this.data.selectorFilter._type==0){
      this.setData({
        projectTypeTip: filterText
      })
      course.getCourseList(this.data.currentReqType, this.data.currentGradeLevel,
        filterId, this.data.currentSelectProjectEdition, this.data.currentPageIndex, 10, function (list) {
          that.setData({
            courses: list
          })
        })
    } else if (this.data.selectorFilter._type == 1){
      this.setData({
        projectEditionTip: filterText
      })
      course.getCourseList(this.data.currentReqType, this.data.currentGradeLevel,
        this.data.currentSelectProjectType, filterId, this.data.currentPageIndex, 10, function (list) {
          that.setData({
            courses: list
          })
        })
    }
    this.setData({
      selectorFilter: this.data.selectorFilter
    })
  },
  /**
   * 筛选
   */
  filtershops: function (event) {
    if (this.data.top_show) {
      _closeTopAnimation(this)
    }
    _openRightAnimation(this)
  }
  ,
  closefilterview: function (event) {
    console.log('closefilterview')
    _closeRightAnimation(this)
  },
  resetFilter: function (event) {
    _closeRightAnimation(this)
  },
  submitFilter: function (event) {
    _closeRightAnimation(this)
  },
  /**
  * 点击某个商品
  */
  onCourseItemClick: function (event) {
    var position = event.detail.position
    var id = event.detail.courseNo
    var title = event.detail.title
    var userName = ''
    if (Number(event.detail.showFullName) == 0) {//显示用户名
      userName = event.detail.teacherName
    } else {//显示全名称
      userName = event.detail.teacheRrealName
    }
    var _url = '../courseinfo/detail?courseId=' + id + '&title=' + title + '&teacherName=' + userName
    wx.navigateTo({
      url: _url,
    })
  }
})
function _openRightAnimation(context) {
  var animation = rightFilterAnimation
  context.animation = animation
  animation.translateX(-100 + 'vh').step() //动画效果向右滑动100vh
  context.setData({
    animationData: animation.export(),
    show: true,
    top_show: false,
    _filterAllFontColor: '#000'
  })
}
/**
 * 关闭右边筛选
 */
function _closeRightAnimation(context) {
  var animation = rightFilterAnimation
  context.animation = animation
  animation.translateX(100 + 'vh').step() //动画效果向右滑动100vh
  context.setData({
    animationData: animation.export(),
    show: true,
    top_show: false,
    _filterAllFontColor: '#FF7256'
  })
}
/**
 * 开启顶部条件
 */
function _openTopAnimation(context,viewId) {
  var animation = topFilterAnimation
  context.animation = animation
  animation.translateY(90 + 'vh').step()
  if (viewId=='projectType'){
    context.setData({
      topAnimationData: animation.export(),
      show: false,
      top_show: true,
      _filterPTypeFontColor: '#FF7256',
    })
  }else{
    context.setData({
      topAnimationData: animation.export(),
      show: false,
      top_show: true,
      _filterPEditionFontColor: '#FF7256',
    })
  }
  
}
/**
 * 关闭顶部条件
 */
function _closeTopAnimation(context,viewId) {
  var animation = topFilterAnimation
  context.animation = animation
  animation.translateY(-90 + 'vh').step()
  if (viewId == 'projectType') {
    context.setData({
      topAnimationData: animation.export(),
      show: false,
      top_show: false,
      _filterPTypeFontColor: '#000',
    })
  } else {
    context.setData({
      topAnimationData: animation.export(),
      show: false,
      top_show: false,
      _filterPEditionFontColor: '#000',
    })
  }
}
